package servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;
import managers.JSonManager;
import structures.ADsList;

public class GetADsServlet extends HttpServlet {

	private static final long serialVersionUID = 1985124165058660491L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		String limit, starterId, method, adType, jsonResult;
		ADsList adsList;
		JSonManager jSonManager;
		jSonManager = new JSonManager();
		DatabaseManager databaseManager;
		databaseManager = new DatabaseManager();
		limit = request.getParameter("limit");
		starterId = request.getParameter("starterId");
		method = request.getParameter("method");
		adType = request.getParameter("adType");
         try {
        	databaseManager.connect();
        	adsList = databaseManager.getADs(limit, starterId, method, adType);
        	jsonResult = jSonManager.generateJSON(adsList);
        	response.setContentType("application/json");
    		response.setCharacterEncoding("UTF-8");
			response.getWriter().write(jsonResult);
			databaseManager.closeConnection();
	        
        } catch (Exception e) {
			e.printStackTrace();
		}
    }
}
